var wx = require("weixin-js-sdk");

function wxConfig(data) {
  wx.config({
    debug: false,
    appId: data.appId,
    timestamp: data.timestamp,
    nonceStr: data.nonceStr,
    signature: data.signature,
    jsApiList: [
      "onMenuShareTimeline",
      "onMenuShareAppMessage",
      "onMenuShareQQ",
      "onMenuShareWeibo",
      "onMenuShareQZone",
      "getLocation",
      "scanQRCode",
      "openLocation",
    ]
  });
  wx.error(function(res){
    console.log(res)
  })
}
// 扫码
function scanQRCode(func) {
    wx.scanQRCode({
      needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
      scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
      success: function(res) {
        func(res);
      }
    });
}
// 获取坐标
function getLocation(func) {
  wx.getLocation({
    type: "gcj02", // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
    success: function(res) {
      func(res);
    }
  });
}
//

function openLocation(data) {
  console.log('openLocation:',data);
  wx.openLocation({
    latitude: data.latitude, // 纬度，浮点数，范围为90 ~ -90
    longitude: data.longitude, // 经度，浮点数，范围为180 ~ -180。
    name: data.name, // 位置名
    address: data.address, // 地址详情说明
    scale: 13, // 地图缩放级别,整形值,范围从1~28。默认为最大
    infoUrl: "" // 在查看位置界面底部显示的超链接,可点击跳转
  });
}


function wxShare(config) {
  //检测api是否生效
    //分享给好友
    wx.onMenuShareAppMessage({
      title: config.title,
      desc: config.desc,
      link: config.link,
      imgUrl: config.imgUrl,
      success: function(res) {
        console.log(1);
        _hmt.push(['_trackEvent', '超级试骑', '分享好友'])

        _hmt.push(['_trackPageview', '/fenxianghaoyou']);
      },
      cancel: function() {
        console.log(2);
      },
      error: function() {
        console.log(3);
      }
    });
    //分享到朋友圈
    wx.onMenuShareTimeline({
      title: config.title,
      desc: config.desc,
      link: config.link,
      imgUrl: config.imgUrl,
      success: function(res) {
        console.log(1);
        _hmt.push(['_trackEvent', '超级试骑', '分享朋友圈'])

        _hmt.push(['_trackPageview', '/pengyouquan']);
      },
      cancel: function() {
        console.log(2);
      },
      error: function() {
        console.log(3);
      }
    });
}
let weixin = {
  wxConfig: wxConfig,
  getLocation: getLocation,
  scanQRCode: scanQRCode,
  openLocation: openLocation,
  wxShare: wxShare
};
export default weixin;